package com.ruoyi.project.system.job.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.courseClass.domain.CourseClass;
import com.ruoyi.project.system.student.domain.ClassStudent;
import com.ruoyi.project.system.student.mapper.ClassStudentMapper;
import com.ruoyi.project.system.submit.domain.JobSubmit;
import com.ruoyi.project.system.submit.mapper.JobSubmitMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.job.mapper.JobCourseClassMapper;
import com.ruoyi.project.system.job.domain.JobCourseClass;
import com.ruoyi.project.system.job.service.IJobCourseClassService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 作业-课程-班级记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-15
 */
@Service
public class JobCourseClassServiceImpl implements IJobCourseClassService {
    @Autowired
    private JobCourseClassMapper jobCourseClassMapper;

    @Autowired
    private JobSubmitMapper jobSubmitMapper;

    @Autowired
    private ClassStudentMapper classStudentMapper;

    /**
     * 查询作业-课程-班级记录
     *
     * @param id 作业-课程-班级记录ID
     * @return 作业-课程-班级记录
     */
    @Override
    public List<JobCourseClass> selectJobCourseClassById(Long id) {
        return jobCourseClassMapper.selectJobCourseClassById(id);
    }

    /**
     * 查询作业-课程-班级记录列表
     *
     * @param jobCourseClass 作业-课程-班级记录
     * @return 作业-课程-班级记录
     */
    @Override
    public List<JobCourseClass> selectJobCourseClassList(JobCourseClass jobCourseClass) {
        List<JobCourseClass> jobCourseClassList = jobCourseClassMapper.selectJobCourseClassListGroupBy(jobCourseClass);
        if (jobCourseClassList.size() > 0) {
            for (JobCourseClass courseClass : jobCourseClassList) {
                // 获取其下所有未提交作业的学生
                JobSubmit condition = new JobSubmit();
                condition.setJobId(courseClass.getId());
                condition.setStatus(3);
                List<JobSubmit> jobSubmitList = jobSubmitMapper.selectJobSubmitList(condition);
                StringBuilder stringBuilder = new StringBuilder();
                if (jobSubmitList.size() > 0) {
                    for (JobSubmit jobSubmit : jobSubmitList) {
                        stringBuilder.append(jobSubmit.getStudentName() + ",");
                    }
                }
                if (StringUtils.isEmpty(stringBuilder.toString())) {
                    courseClass.setStudentNames("所有学生均已提交作业");
                } else {
                    courseClass.setStudentNames(stringBuilder.toString());
                }
            }
        }
        return jobCourseClassList;
    }

    /**
     * 新增作业-课程-班级记录
     *
     * @param jobCourseClass 作业-课程-班级记录
     * @return 结果
     */
    @Override
    public int insertJobCourseClass(JobCourseClass jobCourseClass) {
        Long groupId = RandomUtils.nextLong(10000000000L, 99999999999L);
        List<String> classIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(jobCourseClass.getClassIds())) {
            classIds = Arrays.asList(jobCourseClass.getClassIds().split(","));
        }
        // 生成所有试题
        if (!jobCourseClass.getJobList().isEmpty() && jobCourseClass.getJobList().size() > 0) {
            for (JobCourseClass courseClass : jobCourseClass.getJobList()) {
                courseClass.setCreateTime(DateUtils.getNowDate());
                courseClass.setCourseId(jobCourseClass.getCourseId());
                courseClass.setClassIds(jobCourseClass.getClassIds());
                courseClass.setKnowledgeDescription(jobCourseClass.getKnowledgeDescription());
                courseClass.setKnowledgeDescription(jobCourseClass.getKnowledgeDescription());
                courseClass.setDeductScore(jobCourseClass.getDeductScore());
                courseClass.setBeginTime(jobCourseClass.getBeginTime());
                courseClass.setEndTime(jobCourseClass.getEndTime());
                courseClass.setMakeUpTime(jobCourseClass.getMakeUpTime());
                courseClass.setGroupId(groupId);
                if(courseClass.getType().equals("选择题")){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("option", courseClass.getOption());
                    jsonObject.put("optionanswer", courseClass.getOptionanswer());
                    courseClass.setOptionJson(jsonObject.toJSONString());
                }
                jobCourseClassMapper.insertJobCourseClass(courseClass);
                if (classIds.size() > 0) {
                    for (String classId : classIds) {
                        // 根据班级ID查询所有学生信息
                        ClassStudent condition = new ClassStudent();
                        condition.setClassId(Long.valueOf(classId));
                        List<ClassStudent> studentList = classStudentMapper.selectClassStudentList(condition);
                        if (studentList.size() > 0) {
                            for (ClassStudent classStudent : studentList) {
                                JobSubmit jobSubmit = new JobSubmit();
                                jobSubmit.setGroupId(groupId);
                                jobSubmit.setJobId(courseClass.getId());
                                jobSubmit.setClassId(classStudent.getClassId());
                                jobSubmit.setStudentId(classStudent.getStudentId());
                                jobSubmit.setStatus(3);
                                jobSubmit.setScore(new BigDecimal("0"));
                                jobSubmitMapper.insertJobSubmit(jobSubmit);
                            }
                        }
                    }
                }
            }
        }
        return 1;
    }

    /**
     * 修改作业-课程-班级记录
     *
     * @param jobCourseClass 作业-课程-班级记录
     * @return 结果
     */
    @Override
    public int updateJobCourseClass(JobCourseClass jobCourseClass) {
        Long groupId = jobCourseClass.getGroupId();
        jobCourseClassMapper.deleteByGroupId(groupId);
        jobSubmitMapper.deleteByGroupId(groupId);
        List<String> classIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(jobCourseClass.getClassIds())) {
            classIds = Arrays.asList(jobCourseClass.getClassIds().split(","));
        }
        // 生成所有试题
        if (!jobCourseClass.getJobList().isEmpty() && jobCourseClass.getJobList().size() > 0) {
            for (JobCourseClass courseClass : jobCourseClass.getJobList()) {
                courseClass.setCreateTime(DateUtils.getNowDate());
                courseClass.setCourseId(jobCourseClass.getCourseId());
                courseClass.setClassIds(jobCourseClass.getClassIds());
                courseClass.setKnowledgeDescription(jobCourseClass.getKnowledgeDescription());
                courseClass.setDeductScore(jobCourseClass.getDeductScore());
                courseClass.setBeginTime(jobCourseClass.getBeginTime());
                courseClass.setEndTime(jobCourseClass.getEndTime());
                courseClass.setMakeUpTime(jobCourseClass.getMakeUpTime());
                courseClass.setGroupId(groupId);
                if(courseClass.getType().equals("选择题") ){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("option", courseClass.getOption());
                    jsonObject.put("optionanswer", courseClass.getOptionanswer());
                    courseClass.setOptionJson(jsonObject.toJSONString());
                }
                jobCourseClassMapper.insertJobCourseClass(courseClass);
                if (classIds.size() > 0) {
                    for (String classId : classIds) {
                        // 根据班级ID查询所有学生信息
                        ClassStudent condition = new ClassStudent();
                        condition.setClassId(Long.valueOf(classId));
                        List<ClassStudent> studentList = classStudentMapper.selectClassStudentList(condition);
                        if (studentList.size() > 0) {
                            for (ClassStudent classStudent : studentList) {
                                JobSubmit jobSubmit = new JobSubmit();
                                jobSubmit.setJobId(courseClass.getId());
                                jobSubmit.setGroupId(groupId);
                                jobSubmit.setClassId(classStudent.getClassId());
                                jobSubmit.setStudentId(classStudent.getStudentId());
                                jobSubmit.setStatus(3);
                                jobSubmit.setScore(new BigDecimal("0"));
                                jobSubmitMapper.insertJobSubmit(jobSubmit);
                            }
                        }
                    }
                }
            }
        }
        return 1;
    }

    /**
     * 删除作业-课程-班级记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobCourseClassByIds(String ids) {
        return jobCourseClassMapper.deleteJobCourseClassByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除作业-课程-班级记录信息
     *
     * @param id 作业-课程-班级记录ID
     * @return 结果
     */
    @Override
    public int deleteJobCourseClassById(Long id) {
        return jobCourseClassMapper.deleteJobCourseClassById(id);
    }
}
